<template>
  <div class="account-data">
    <div class="add-box">
      <el-button @click="addAccount" type="primary">新增</el-button>
    </div>
    <!-- 数据列表 -->
    <el-table :data="tableData" border style="width:100%" v-loading="loading" element-loading-text="拼命加载中">
      <el-table-column label="上传时间" width="180">
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.Yuploadtime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="对应客户手机" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.userTel}}</span>
        </template>
      </el-table-column>

      <el-table-column label="上传摄影师姓名">
        <template slot-scope="scope">
          <span>{{scope.row.Sname}}</span>
        </template>
      </el-table-column>

      <el-table-column label="影集存放地址">
        <template slot-scope="scope">
          <span>
            <!-- <img width="200px" :src="scope.row.Yaddress" /> -->
            <video width="200px" controls>
              <source :src="scope.row.Yaddress" type="video/mp4">
            您的浏览器不支持Video标签。
            </video>
          </span>
        </template>
      </el-table-column>

      <el-table-column label="修改时间">
        <template slot-scope="scope">
          <span>{{ scope.row.Yupdatetime ==='' ? scope.row.Yupdatetime : formatDate(scope.row.Yupdatetime)}}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注">
        <template slot-scope="scope">
          <span>{{scope.row.Ybeizhu}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" size="mini" type="danger">删除</el-button>
          <el-button @click="handleEdit(scope.row)" size="mini">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pages" ref="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        layout="prev, pager, next"
        :page-size="pages"
        :total="total">
      </el-pagination>
    </div>

    <!-- 新增 -->
    <el-dialog
      title="新增"
      :visible.sync="dialogVisibleAdd"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form :rules="SgradeFrom" ref="AddForm" :model="listObj" class="form-box" >
        <el-form-item label="对应客户手机" prop="userTel">
          <el-input v-model="listObj.userTel" placeholder="请输入手机号码"></el-input>
        </el-form-item>


        <div class="files">
          <div>+</div>
          <input class="files-con" type="file"  @change="userUpload($event)"  />
          <video v-if="listObj.Yaddress" class="files-img" controls>
              <source :src="listObj.Yaddress" type="video/mp4">
            您的浏览器不支持Video标签。
          </video>
        </div>

        <el-form-item label="备注" prop="Ybeizhu">
          <el-input v-model="listObj.Ybeizhu" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button @click="handleAdd" type="primary" :loading="loading">确定</el-button>
      </span>
    </el-dialog>


    <!-- 编辑 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisibleEdit"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <el-form :rules="SgradeFrom" ref="EditForm" :model="listObj" class="form-box" >
        <el-form-item label="对应客户手机" prop="userTel">
          <el-input v-model="listObj.userTel" placeholder="请输入手机号码"></el-input>
        </el-form-item>

        <div class="files">
          <div>+</div>
          <input class="files-con" type="file"  @change="userUpload($event)"  />
          <video v-if="listObj.Yaddress" class="files-img" controls>
              <source :src="listObj.Yaddress" type="video/mp4">
            您的浏览器不支持Video标签。
          </video>
        </div>

        <el-form-item label="备注" prop="Ybeizhu">
          <el-input v-model="listObj.Ybeizhu" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button @click="handleEditSave()" type="primary" :loading="loading">确定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import jwt_decode from 'jwt-decode';

@Component({
  components: {  }
})
export default class AccountData extends Vue {
  getUser: any;
  tableData: any = [];
  total:number = 0; // 总数据量
  pages:number = 3; // 每页数据量
  pageNumber: number = 0; // 页码
  loading:boolean = true;// loading加载
  dialogVisibleAdd: Boolean = false; // 新增模态框显示状态
  dialogVisibleEdit: Boolean = false; // 编辑模态框显示状态
  Yingjiid:Number = 0; // 数据ID

  // 对象数据
  listObj: any = {
    userTel:'', Sname:'', Yaddress:'', Ybeizhu:'', Sheyinshiid:''
  };

  SgradeFrom: any = {
    userTel: [{ required: true, message: "请输入手机号码", trigger: "change" }],
  };


  created() {
    this.getUser = jwt_decode(localStorage.tsToken);
    this.getData();
  }

  // 新增账户
  handleAdd() {
    (this as any).$refs["AddForm"].validate((valid: boolean) => {
      if (valid) {
        this.loading = true;
        this.listObj.Sname = this.getUser.nickName;
        this.listObj.Sheyinshiid = this.getUser.id;
        const param:any = this.listObj;

        (this as any).$axios.post("/adminYingji/add", param).then((res: any) => {
          if(res.data.code === 200){
            this.getData();
            this.closeDialog();
            this.loading = false;
            (this as any).$message({
              message: '添加成功',
              type: "success"
            })
          }
          else{
            this.closeDialog();
            this.loading = false;
            (this as any).$message.error(res.data.data)
          }
        })
      }
    });
  }


  // 显示新增账户模态框
  addAccount() {
    this.listObj.userTel = ''; 
    this.listObj.Sname = ''; 
    this.listObj.Yaddress = ''; 
    this.listObj.Ybeizhu = ''; 
    this.listObj.Sheyinshiid = '';
    
    this.dialogVisibleAdd = true;
  }

  // 隐藏新增账户
  closeDialog() {
    this.dialogVisibleAdd = false;
    this.dialogVisibleEdit = false;
  }

  // 删除
  handleDelete(row: any): void {
    (this as any).$confirm('是否确认删除？')
    .then((res:any)=> {
      const { Yingjiid } = row;

      (this as any).$axios.post('/adminYingji/del', { Yingjiid }).then((res: any) => {
        if(res.data.code === 200){
            this.getData();
            (this as any).$message({
              message: res.data.data,
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
      })
    }) 
  }

  handleEdit(row: any){
    this.dialogVisibleEdit = true;
    const { userTel, Yaddress, Ybeizhu, Yingjiid } = row;
    
    this.listObj.userTel = userTel;
    this.listObj.Yaddress = Yaddress;
    this.listObj.Ybeizhu = Ybeizhu;
    this.Yingjiid = Yingjiid;
  }
  // 编辑保存
  handleEditSave(){
    (this as any).$refs["EditForm"].validate((valid: boolean) => {
      if (valid) {
        this.loading = true;
        const { userTel, Yaddress, Ybeizhu } = this.listObj;
        const param:any = {
          userTel, Yaddress, Ybeizhu 
        };
        param.Yingjiid = this.Yingjiid;

        (this as any).$axios.post("/adminYingji/update", param).then((res: any) => {
          if(res.data.code === 200){
            this.getData();
            this.closeDialog();
            this.loading = false;
            (this as any).$message({
              message: '修改成功',
              type: "success"
            })
          }
          else{
            (this as any).$message.error(res.data.data)
          }
        })
      }
    });
  }

  // 数据显示
  getData() {
    this.loading = true;
    const param = {
      pages: this.pages,
      pageNumber: this.pageNumber
    };

    (this as any).$axios("/adminYingji/dataList", { params:param }).then((res: any) => {
      this.loading = false;

      if(res.data.code === 200){
        this.tableData = res.data.data.list;
        this.total = res.data.data.sum;
      }
    })
  }

  // 分页 pageSize 改变时会触发
  handleSizeChange(val: number) {
    if(val === 1){
      this.pageNumber = 0
    }
    else{
      this.pageNumber = (val-1) * this.pages;
    }
    this.getData();
  }

  // 分页 currentPage 改变时会触发
  handleCurrentChange(val: number) {
    if(val === 1){
      this.pageNumber = 0
    }
    else{
      this.pageNumber = (val-1) * this.pages;
    }
    this.getData();
  }

  /**
   * @param {数值类型} date
   */
  formatDate(date:any){
      date = new Date(date*1);
      var y  =  date.getFullYear();
      var m  =  date.getMonth()+1;
      var d  =  date.getDate();
      var h  =  date.getHours();
      var m1  =  date.getMinutes();
      var s  =  date.getSeconds();
      m  =  m<10?("0"+m):m;
      d  =  d<10?("0"+d):d;
      return y+"-"+m+"-"+d+" "+h+":"+m1+":"+s;
  }

  //头像上传  
  //将图片以文件的形式上传
	userUpload(e:any){
			let file = e.target.files[0];
			let param = new FormData();
			param.append('file',file);// 通过append 向from对象添加数据
			// FormData私有类对象，访问不到，可以通过get判断值是否传进去 console.log(param.get("file"));
			
			(this as any).$axios.post('/upload',param,{
				headers:{
					"Content-Type":"multipart/form-data",//添加请求头
				}
			})
			.then((res:any)=>{
        if(res.data.code === 200){
          this.listObj.Yaddress = res.data.data;
          return;
        }
        alert('文件上传失败，请稍后上传');
			})
      .catch((error:any)=>{
				alert('文件过大, 请稍后上传');
			})
	}
}
</script>

<style lang="scss" scoped>
.files{
  width: 200px;
  height: 200px;
  margin-bottom: 20px;
  position: relative;
  font-size: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed #0f6674;

  &-con{
    width: 200px;
    height: 200px;
    position: absolute;
    opacity: 0;
    cursor: pointer;
  }
  &-img{
    width: 200px;
    height: 200px;
    position: absolute;
    left: 220px;
  }
}

.account-data {
  height: 100%;
  overflow: auto;
  .add-box {
    margin-bottom: 10px;
  }
}
.pages{
  width: 100%;
  margin-top: 10px;
  background: white;
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
}

.form-box {
  .el-input,
  .el-select {
    width: 400px !important;
  }
}
</style>
